/********************************************************************************
 * @Author: Zhenzhou Liu
 * @Date: 2024-12-04
 * @Description: Cg动态规划练习之最长公共子串
 *******************************************************************************/
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

pair<int, int> longestCommonSubstring(const vector<char>& xList, const vector<char>& yList) {
    int m = xList.size(), n = yList.size();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    int lMax = 0, pMax = 0;
    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (xList[i] == yList[j]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
                if (dp[i][j] > lMax) {
                    lMax = dp[i][j];
                    pMax = i;
                }
            }
        }
    }
    return {pMax, lMax};
}

void printLongestCommonSubstring(const vector<char>& xList, pair<int, int> result) {
    if (result.second) {
        int start = result.first - result.second + 1;
        for (int i = start; i <= result.first; ++i) {
            if (i > start) {
                cout << ',';
            }
            cout << xList[i];
        }
    }
}

int main() {
    string input;
    getline(cin, input);

    size_t semicolonPos = input.find(';');
    string prevStr = input.substr(0, semicolonPos);
    string nextStr = input.substr(semicolonPos + 1);

    stringstream ss1(prevStr), ss2(nextStr);
    vector<char> xList, yList;
//    xList, yList与dp索引保持一致
    xList.push_back('\0');
    yList.push_back('\0');
    char temp;
    while (ss1 >> temp) {
        if (ss1.peek() == ',') {
            ss1.ignore();
        }
        xList.push_back(temp);
    }
    while (ss2 >> temp) {
        if (ss2.peek() == ',') {
            ss2.ignore();
        }
        yList.push_back(temp);
    }

    pair<int, int> result = longestCommonSubstring(xList, yList);
    printLongestCommonSubstring(xList, result);
    return 0;
}